package com.masterclient.app.modules.income;

import android.content.Intent;
import android.graphics.Color;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.eoner.baselib.utils.toast.ToastUtil;
import com.eoner.managerlibrary.router.AroutePath;
import com.eoner.managerlibrary.sobot.SobotManager;
import com.masterclient.app.BaseFragment;
import com.masterclient.app.R;
import com.masterclient.app.base.FragmentHostActivity;
import com.masterclient.app.modules.income.contract.UnClearAccountDetailsContract;
import com.masterclient.app.modules.income.contract.UnClearAccountDetailsPresenter;
import com.masterclient.app.modules.income.model.UnClearAccountDetailsBean;
import com.masterclient.app.modules.order.OrderDetailActivity;
import com.sobot.chat.SobotApi;
import com.sobot.chat.api.enumtype.SobotChatTitleDisplayMode;
import com.sobot.chat.api.model.Information;

import java.util.Objects;

import butterknife.BindView;
import butterknife.OnClick;

import static com.masterclient.app.modules.income.WithdrawDetailsFragment.PAGE_TYPE;
import static com.masterclient.app.modules.income.WithdrawDetailsFragment.WITHDRAW_DETAILS;

@Route(path = AroutePath.INCOME_DETAILS_FRAGMENT)
public class InComeDetailsFragment extends BaseFragment<UnClearAccountDetailsPresenter> implements UnClearAccountDetailsContract.View {

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.iv_tag)
    ImageView ivTag;
    @BindView(R.id.tv_account)
    TextView tvAccount;
    @BindView(R.id.tv_title_des)
    TextView tvTitleDes;
    @BindView(R.id.view_content)
    LinearLayout viewContent;

    @Autowired(name = "book_id")
    String bookId;
    @Autowired(name = "bizType")
    String bizType;

    @Override
    protected View getRootView(LayoutInflater inflater) {
        return inflater.inflate(R.layout.fragment_income_details, null);
    }

    @Override
    protected void initOnlyOnce() {
        super.initOnlyOnce();

        ARouter.getInstance().inject(this);
        if ("5".equals(bizType)) {
            tvTitle.setText("提现退回详情");
            mPresenter.getInComeBankDetail(bookId);
        } else {
            tvTitle.setText("收入详情");
            mPresenter.getInComeDetail(bookId);
        }
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @OnClick({R.id.back})
    void onClick(View view) {
        if (view.getId() == R.id.back) {
            Objects.requireNonNull(getActivity()).finish();
        }
    }

    @Override
    public void showData(UnClearAccountDetailsBean dataBean) {

    }

    @Override
    public void showInComeData(UnClearAccountDetailsBean dataBean) {
        if (dataBean != null) {
            if ("5".equals(bizType)) {
                ivTag.setImageResource(R.mipmap.icon_withdraw_return);
            } else {
                ivTag.setImageResource(R.mipmap.icon_withdraw_success);
            }
            tvAccount.setText(dataBean.amount);
            tvTitleDes.setText(dataBean.detailTitle);

            initContentListInfo(dataBean);
        }
    }

    private void initContentListInfo(UnClearAccountDetailsBean dataBean) {
        if (dataBean.contentList != null && dataBean.contentList.size() > 0) {
            for (int i = 0; i < dataBean.contentList.size(); i++) {
                View view = View.inflate(mContext, R.layout.item_un_clear_account_info, null);
                TextView tvLeftTxt = view.findViewById(R.id.tv_left_txt);
                TextView tvRightTxt = view.findViewById(R.id.tv_right_txt);

                tvLeftTxt.setText(dataBean.contentList.get(i).title);
                if ("0".equals(dataBean.contentList.get(i).jumpType)) {
                    tvRightTxt.setTextColor(Color.parseColor("#ff525866"));
                } else if ("3".equals(dataBean.contentList.get(i).jumpType)) {
                    tvRightTxt.setTextColor(Color.parseColor("#E79635"));
                    tvRightTxt.setBackgroundResource(R.drawable.text_underline_yellow);
                    tvRightTxt.setOnClickListener(v -> {
                        //客服
                        Information info = SobotManager.getBaseInfo();
                        SobotApi.setChatTitleDisplayMode(mContext, SobotChatTitleDisplayMode.Default, "");
                        SobotApi.startSobotChat(getActivity(), info);
                    });
                } else {
                    tvRightTxt.setTextColor(Color.parseColor("#397BE6"));
                    tvRightTxt.setCompoundDrawablesWithIntrinsicBounds(null, null, mContext.getResources().getDrawable(R.mipmap.arrow_right_blue), null);
                    tvRightTxt.setCompoundDrawablePadding(2);
                    int finalI = i;
                    tvRightTxt.setOnClickListener(v -> {
                        if ("1".equals(dataBean.contentList.get(finalI).jumpType)) {
                            //销售订单详情
                            ARouter.getInstance().build(AroutePath.SALE_ORDER_DETAIL_ACTIVITY)
                                    .withString("order_no", dataBean.contentList.get(finalI).jump)
                                    .navigation();
                        } else if ("2".equals(dataBean.contentList.get(finalI).jumpType)) {
                            //采购订单详情
                            Intent intent = new Intent(mContext, OrderDetailActivity.class);
                            intent.putExtra("order_no", dataBean.contentList.get(finalI).jump);
                            intent.putExtra("from_type", "purchase");
                            mContext.startActivity(intent);
                        } else if ("4".equals(dataBean.contentList.get(finalI).jumpType)) {
                            //提现详情
                            Fragment fragment = (Fragment) ARouter.getInstance().build(AroutePath.WITHDRAW_DETAILS_FRAGMENT)
                                    .withString(PAGE_TYPE, WITHDRAW_DETAILS)
                                    .withString("withdraw_id", dataBean.contentList.get(finalI).jump)
                                    .navigation();
                            FragmentHostActivity.openFragment(getActivity(), fragment);
                        }
                    });
                }
                tvRightTxt.setText(dataBean.contentList.get(i).content);
                viewContent.addView(view);
            }
            viewContent.setVisibility(View.VISIBLE);
        } else {
            viewContent.setVisibility(View.GONE);
        }
    }

    @Override
    public void showDataFailed(String msg) {
        ToastUtil.showToast(msg);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}
